package com.study.ui.home.adapter

import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.DiffUtil
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lbj.core.adapter.CoreMultiAdapter
import com.lbj.core.adapter.Item
import com.lbj.core.adapter.SuperItem
import com.lbj.core.ext.toToast
import com.lbj.core.tool.data.SimpleTitle
import com.study.R
import com.study.databinding.AdapterItem01Binding
import com.study.databinding.AdapterItem02Binding

/**
 * 作者：李炳军
 * 创建日期：2021-03-30  15:53
 * 释义：
 */
class MainMultiAdapter : CoreMultiAdapter<SimpleTitle>() {

    init {
        addItemType(Item.Item_01, R.layout.adapter_item_01)
        addItemType(Item.Item_02, R.layout.adapter_item_02)
    }

    override fun convert(holder: BaseViewHolder, item: SuperItem) {
        when (item.item) {
            Item.Item_01 -> {
                DataBindingUtil.bind<AdapterItem01Binding>(holder.itemView)?.apply {
                    index = holder.adapterPosition
                    result = item.data as SimpleTitle
                    presenter = Presenter()
                    executePendingBindings()
                }
            }
            Item.Item_02 -> {
                DataBindingUtil.bind<AdapterItem02Binding>(holder.itemView)?.apply {
                    index = holder.adapterPosition
                    result = item.data as SimpleTitle
                    presenter = Presenter()
                    executePendingBindings()
                }
            }
        }
    }

    inner class Presenter {
        fun onItemClick(v: View, index: Int, msg: String) {
            msg.toToast()
            val news = ArrayList<SuperItem>()
            data.map {
                (it.data as SimpleTitle)
                news.add(it.copy())
            }
            (news[index].data as SimpleTitle).apply {
                title = System.currentTimeMillis().toString()
                select = !select
            }
            setDiffNewData(news)
        }
    }

    override fun isItemSame(old: SimpleTitle, new: SimpleTitle) = old.id == new.id

    override fun isContentsSame(old: SimpleTitle, new: SimpleTitle): Boolean {
        return  old.title == new.title && old.select == new.select
    }
}